//
//  BBActionSheet.swift
//  BusinessBao
//
//  Created by Chen on 2017/7/20.
//  Copyright © 2017年 Chen. All rights reserved.
//

import UIKit
import RxSwift

class BBActionSheet: UIView {
    var tapGesture: UITapGestureRecognizer!
    var containerView: UIView!
    var dismissBlock: (() -> ())?
    var selectBlock: ((_ index: Int) -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
    }
    
    func initView() {
        self.tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(tapGrayView(gesture:)));
        self.addGestureRecognizer(self.tapGesture)
        
        self.containerView = UIView(frame: CGRect(x: 0, y: self.height, width: self.width, height: self.height))
        self.containerView.backgroundColor = App_Line_Color
        self.addSubview(self.containerView)
    }
    

    /// 显示类似weChat的actionSheet
    ///
    /// - Parameters:
    ///   - buttonTitles: 按钮title数组(不包括取消)
    ///   - cancelBtnTitle: 取消按钮title
    ///   - selectBlock: 选择了按钮后的回调，回传按钮的index
    ///   - dismissBlock: view隐藏的回调
    func showActionSheet(_ buttonTitles: Array<String>,
                         cancelBtn cancelBtnTitle: String,
                         selectBlock: @escaping ((_ index: Int) -> ()),
                         dismissBlock: @escaping (() -> ())) {
        self.dismissBlock = dismissBlock
        self.selectBlock = selectBlock
        
        let kButtonHeight: CGFloat = kRatio(number: 49)
        
        self.containerView.height = CGFloat(buttonTitles.count) * kButtonHeight + 8 + kButtonHeight
        
        for i in 0..<buttonTitles.count {
            let button = BBActionSheetButton(frame: CGRect(x: 0, y: CGFloat(i) * kButtonHeight, width: self.containerView.width, height: kButtonHeight))
            button.setTitle(buttonTitles[i], for: .normal)
            button.tag = i
            button.addTarget(self, action: #selector(tapTitle(button:)), for: .touchUpInside)
            
            self.containerView.addSubview(button)
            
            let bottomLine = UIView(frame: CGRect(x: 0, y: button.bottom - OnePixelLineWidth, width: button.width, height: OnePixelLineWidth))
            bottomLine.backgroundColor = App_Line_Color
            self.containerView.addSubview(bottomLine)
        }
        
        let cancelButton = BBActionSheetButton(frame: CGRect(x: 0, y: self.containerView.height - kButtonHeight, width: self.containerView.width, height: kButtonHeight))
        cancelButton.setTitle(cancelBtnTitle, for: .normal)
        cancelButton.addTarget(self, action: #selector(dismissActionSheet), for: .touchUpInside)
        self.containerView.addSubview(cancelButton)
        
        self.showActionSheet()
    }
    
    func tapTitle(button: BBActionSheetButton) {
        if (self.selectBlock != nil) {
            self.selectBlock!(button.tag)
            self.dismissActionSheet()
        }
    }
    
    func tapGrayView(gesture: UITapGestureRecognizer) {
        let tapY = gesture.location(in: self).y
        if (tapY < self.containerView.top) {
            self.dismissActionSheet()
        }
    }
    
    func showActionSheet() {
        UIView.animate(withDuration: 0.25, animations: { 
            self.backgroundColor = RGBA(R: 0, G: 0, B: 0, A: 0.3)
            self.containerView.top = self.height - self.containerView.height - CGFloat(MarginBottom)
        }) { (finished) in
            
        }
    }
    
    func dismissActionSheet() {
        UIView.animate(withDuration: 0.25, animations: { 
            self.backgroundColor = UIColor.clear
            self.containerView.top = self.height
        }) { (finished) in
            if ((self.dismissBlock) != nil) {
                self.removeGestureRecognizer(self.tapGesture)
                self.dismissBlock!()
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
